package com.sf.marathon.soul.core.base;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sf.marathon.soul.core.util.Collections3;
import com.sf.marathon.soul.core.util.ReflectUtils;
import com.sf.marathon.soul.core.util.StringUtils;
import com.sf.marathon.soul.core.util.WebUtils;

/**
 * @author 01368146<br>
 *         BaseController<br>
 *         基础Controller（ 泛型：M 是 Service 对象，T 是实体 ， PK 是主键泛型 ）<br>
 * @since 2017/6/14<br>
 */
public abstract class BaseController<T> {

    /**
     * 基础服务
     */
    protected BaseService baseService;
    protected Class<T> entityClazz;
    /**
     * 前端mainVO的class
     */
    protected Class<?> mainVOClazz = null;

    /**
     * 设置mainVo
     *
     * @param mainVOClazz
     */
    public void setMainVOClazz(Class<?> mainVOClazz) {
        this.mainVOClazz = mainVOClazz;
    }


    @PostConstruct
    protected void initConfig() {
        entityClazz = ReflectUtils.getClassGenricType(getClass());
        init();
    }

    /**
     * 抽象的初始化，需有子类实现
     */
    protected abstract void init();


    /**
     * 分页查询，网格数据
     *
     * @param searchable
     * @param request
     * @return
     */
    @RequestMapping(value = "/find")
    @ResponseBody
    public Map<String, Object> find(Searchable searchable, @RequestParam(value = "vo", required = false) String vo, HttpServletRequest request) {
        Map<String, Object> resultMap = Maps.newHashMap();
        List rows = baseService.selectList((Wrapper) searchable);
        if (Collections3.isEmpty(rows)) {
            rows = Lists.newArrayList();
        }
        resultMap.put("rows", rows);
        resultMap.put("total", WebUtils.freePageTotal());

        return resultMap;
    }

    /**
     * 查询所有，不分页，返回list对象
     *
     * @param searchable
     * @param request
     * @return
     */
    @RequestMapping(value = "/findAll")
    @ResponseBody
    public List findAll(Searchable searchable, @RequestParam(value = "vo", required = false) String vo, HttpServletRequest request) {
        beforeSelectList(searchable);
        Wrapper ew = (Wrapper) searchable;
        List rows = baseService.selectList(ew);
        if (Collections3.isEmpty(rows)) {
            rows = Lists.newArrayList();
        }
        return rows;
    }

    /**
     * 查询之前，供子类覆写，针对需要处理某些特定查询条件的情况
     *
     * @param searchable
     */
    protected void beforeSelectList(Searchable searchable) {

    }

    /**
     * 查询单行
     *
     * @param searchable
     * @param request
     * @return
     */
    @RequestMapping(value = "/findOne")
    @ResponseBody
    public Object findOne(Searchable searchable, @RequestParam(value = "vo", required = false) String vo, HttpServletRequest request) {
        Object one = baseService.selectOne((Wrapper) searchable);
        return one;
    }

    /**
     * 保存单行
     *
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public String save(@RequestBody SaveRequest<T> reqData, HttpServletRequest request) {
        String result = baseService.save(reqData);
        return StringUtils.isNotEmpty(result) ? result : "success";
    }
}
